

/* ----------------------- System includes ----------------------------------*/
#include "stdlib.h"
#include "string.h"

/* ----------------------- Platform includes --------------------------------*/

/* ----------------------- Modbus includes ----------------------------------*/
#include "Muti_MB_config.h"

/* ----------------------- Defines ------------------------------------------*/
#define MUTI_MB_PDU_FUNC_READ_ADDR_OFF           ( MUTI_MB_PDU_DATA_OFF )		//���Ĵ����ĵ�ַ��ƫ��
#define MUTI_MB_PDU_FUNC_READ_REGCNT_OFF         ( MUTI_MB_PDU_DATA_OFF + 2 )	//���Ĵ�����������ƫ��
#define MUTI_MB_PDU_FUNC_READ_SIZE               ( 4 )
#define MUTI_MB_PDU_FUNC_READ_REGCNT_MAX         ( 0x007D )

#define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF    ( MUTI_MB_PDU_DATA_OFF )

/* ----------------------- Static functions ---------------------------------*/
eMUTI_MB_Exception    __MUTI_MB_Error2Exception( eMUTI_MB_ErrorCode eErrorCode );

/* ----------------------- Start implementation -----------------------------*/
#if MUTI_MB_FUNC_READ_INPUT_ENABLED > 0

eMUTI_MB_Exception
eMUTI_MB_FuncReadInputRegister(MUTI_MB_object_t *object,  UCHAR * pucFrame, USHORT * usLen )
{
    USHORT          usRegAddress;
    USHORT          usRegCount;
    UCHAR          *pucFrameCur;

    eMUTI_MB_Exception    eStatus = MUTI_MB_EX_NONE;
    eMUTI_MB_ErrorCode    eRegStatus;

    if( *usLen == ( MUTI_MB_PDU_FUNC_READ_SIZE + MUTI_MB_PDU_SIZE_MIN ) )
    {
        usRegAddress = ( USHORT )( pucFrame[MUTI_MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
        usRegAddress |= ( USHORT )( pucFrame[MUTI_MB_PDU_FUNC_READ_ADDR_OFF + 1] );
        //usRegAddress++;

        usRegCount = ( USHORT )( pucFrame[MUTI_MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
        usRegCount |= ( USHORT )( pucFrame[MUTI_MB_PDU_FUNC_READ_REGCNT_OFF + 1] );

        /* Check if the number of registers to read is valid. If not
         * return Modbus illegal data value exception. 
         */
        if( ( usRegCount >= 1 )
            && ( usRegCount < MUTI_MB_PDU_FUNC_READ_REGCNT_MAX ) )	//���Ĵ������������ ������0X7D
        {
            /* Set the current PDU data pointer to the beginning. */
            pucFrameCur = &pucFrame[MUTI_MB_PDU_FUNC_OFF];
            *usLen = MUTI_MB_PDU_FUNC_OFF;

            /* First byte contains the function code. */
            *pucFrameCur++ = MUTI_MB_FUNC_READ_INPUT_REGISTER;
            *usLen += 1;

            /* Second byte in the response contain the number of bytes. */
            *pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
            *usLen += 1;


              if(object->protocol_cb != NULL)
            {
                eRegStatus = object->protocol_cb(object->function_code, pucFrameCur, usRegAddress, usRegCount, MB_REG_READ);
            }

            // eRegStatus =
            //     eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );

            /* If an error occured convert it into a Modbus exception. */
            if( eRegStatus != MUTI_MB_ENOERR )
            {
                eStatus = __MUTI_MB_Error2Exception( eRegStatus );
            }
            else
            {
                *usLen += usRegCount * 2;
            }
        }
        else
        {
            eStatus = MUTI_MB_EX_ILLEGAL_DATA_VALUE;
        }
    }
    else
    {
        /* Can't be a valid read input register request because the length
         * is incorrect. */
        eStatus = MUTI_MB_EX_ILLEGAL_DATA_VALUE;
    }
    return eStatus;
}

#endif
